﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Rally_like_a_madman.Data;
using System.Collections.ObjectModel;

namespace Rally_like_a_madman
{
    public partial class AddExistingWaypointPage : PhoneApplicationPage
    {
        public Route activeRoute;
        public ObservableCollection<RouteWaypointLink> routeWaypointLinks;
        public ObservableCollection<Waypoint> lbExistingWaypointsSource;

        /// <summary>
        /// 
        /// </summary>
        public AddExistingWaypointPage()
        {
            InitializeComponent();

            App thisApp = Application.Current as App;
            var tmpWaypoints = from Waypoint waypoint in thisApp.ActiveDB.WaypointTable select waypoint;

            lbExistingWaypointsSource = new ObservableCollection<Waypoint>(tmpWaypoints.ToList());

            lbExistingWaypoints.ItemsSource = lbExistingWaypointsSource;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddWaypoint_Click(object sender, RoutedEventArgs e)
        {
            if (lbExistingWaypoints.SelectedItem != null)
            {
                RouteWaypointLink routeWaypointLink = new RouteWaypointLink();
                routeWaypointLink.Route = activeRoute;
                routeWaypointLink.Waypoint = lbExistingWaypoints.SelectedItem as Waypoint;
                routeWaypointLink.WaypointIndex = routeWaypointLinks.Count;
                routeWaypointLinks.Add(routeWaypointLink as RouteWaypointLink);
            }

            NavigationService.GoBack();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveFromDatabase_Click(object sender, RoutedEventArgs e)
        {
            Waypoint selectedWaypoint = lbExistingWaypoints.SelectedItem as Waypoint;

            MessageBoxResult m = MessageBox.Show("Weet je zeker dat je de geselecteerde Waypoint (" + selectedWaypoint.Name
                + ") uit de database wil verwijderen?\n\nDit is definitief en kan niet ongedaan gemaakt worden."
                + "\n\nDe geselecteerde Waypoint zal tevens verwijdert worden uit elke route.",
                "Waypoint verwijderen uit database", MessageBoxButton.OKCancel);

            if (m == MessageBoxResult.OK)
            {
                App thisApp = Application.Current as App;

                #region Remove RouteWaypointLinks from database
                //retrieve all existing RouteWayPointLinks
                var routeWaypointLinksDB = from RouteWaypointLink routeWaypointLink in thisApp.ActiveDB.RouteLinkTable select routeWaypointLink;
                //convert the var to a List<RouteWaypointLink>
                List<RouteWaypointLink> existingRouteWaypointLinks = routeWaypointLinksDB.ToList() as List<RouteWaypointLink>;

                //delete every RouteWaypointLink where the IDWaypoint is the same as the selected IDWaypoint
                foreach (RouteWaypointLink r in existingRouteWaypointLinks)
                {
                    if (r.Waypoint.IDWaypoint == selectedWaypoint.IDWaypoint)
                        thisApp.ActiveDB.RouteLinkTable.DeleteOnSubmit(r);
                }
                #endregion

                #region Remove Waypoint from database
                //retrieve all existing Waypoints
                var waypointsDB = from Waypoint waypoint in thisApp.ActiveDB.WaypointTable select waypoint;
                //convert the var to a List<Waypoint>
                List<Waypoint> existingWaypoints = waypointsDB.ToList() as List<Waypoint>;

                //delete every Waypoint where the IDWaypoint is the same as the selected IDWaypoint
                foreach (Waypoint w in existingWaypoints)
                {
                    if (w.IDWaypoint == selectedWaypoint.IDWaypoint)
                        thisApp.ActiveDB.WaypointTable.DeleteOnSubmit(w);
                }

                thisApp.ActiveDB.SubmitChanges();
                #endregion

                #region Remove Waypoint from waypoints list
                //copy every Waypoint from waypoints to a temporary array if the IDWaypoint == selectedWaypoints.IDWaypoint
                var waypointLinksToRemove = this.routeWaypointLinks.Where(w => w.Waypoint.IDWaypoint == selectedWaypoint.IDWaypoint).ToArray();

                //remove every Waypoint in waypointsToRemove from the waypoints list
                foreach (var routeWaypointLink in waypointLinksToRemove)
                    this.routeWaypointLinks.Remove(routeWaypointLink);
                #endregion

                #region Remove Waypoint from lbExistingWaypointsSource
                //copy every Waypoint from lbExistingWaypointsSource to a temporary array if the IDWaypoint == selectedWaypoints.IDWaypoint
                var waypointsToRemove = this.lbExistingWaypointsSource.Where(w => w.IDWaypoint == selectedWaypoint.IDWaypoint).ToArray();

                //remove every Waypoint in waypointsToRemove from the lbExistingWaypointsSource list
                foreach (var waypoint in waypointsToRemove)
                    this.lbExistingWaypointsSource.Remove(waypoint);
                #endregion
            }
        }
    }
}